package com.example.basic.web.service.role.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.basic.base.impl.BaseEntityServiceImpl;
import com.example.basic.exception.ServiceException;
import com.example.basic.utils.PinyinUtils;
import com.example.basic.utils.StringUtils;
import com.example.basic.utils.WubiUtils;
import com.example.basic.web.enums.DataCodeEnum;
import com.example.basic.web.mapper.role.RoleMapper;
import com.example.basic.web.model.basicData.BasicData;
import com.example.basic.web.model.role.Role;
import com.example.basic.web.model.user.User;
import com.example.basic.web.service.role.RoleService;
import com.example.basic.web.service.roleVsMenu.RoleVsMenuService;
import com.example.basic.web.service.dataCode.DataCodeService;
import com.example.basic.web.service.user.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

import static com.example.basic.utils.CheckUtil.notNull;


@Service
public class RoleServiceImpl extends BaseEntityServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private DataCodeService dataCodeService;

    @Resource
    private RoleVsMenuService roleVsMenuService;

    @Resource
    private UserService userService;

    /**
     * 新增或更新
     * @param role
     * @return
     */
    @Override
    public Role addOrUpdate(Role role) {
        notNull(role, "参数不能为空");
        notNull(role.getRoleName(),"角色名称不能为空");

        //角色编码不为空，修改角色信息
        if(StringUtils.notEmptyParams(role.getRoleCode())){
            //校验角色名称唯一
            LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Role::getRoleName,role.getRoleName());
            lqw.ne(Role::getRoleCode,role.getRoleCode());
            Role localRole = this.mapper.selectOne(lqw);
            if(localRole != null){
                throw new ServiceException("角色名称已存在");
            }
            preUpdate(role);
        }else {
            //校验角色名称唯一
            LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Role::getRoleName,role.getRoleName());
            Role localRole = this.mapper.selectOne(lqw);
            if(localRole != null){
                throw new ServiceException("角色名称已存在");
            }

            preCreate(role);
            //生成角色编码
            String roleCode = dataCodeService.generateDataCodeNo(DataCodeEnum.ROLE_CODE.getType());
            role.setRoleCode(roleCode);
        }

        //生成拼音码五笔码
        generatePyWbCode(role);

        save(role);

        return role;
    }

    /**
     * @create 生成五笔码、拼音码
     * @param role 模型实体类
     */
    private void generatePyWbCode(Role role) {
        // 获取角色名
        String roleName = StringUtils.safeToString(role.getRoleName(), "");
        // 生成拼音码
        if("".equals(StringUtils.safeToString(role.getPyCode(),"").replaceAll(" ",""))) {
            String userNamePinyin = PinyinUtils.getSimplePinYin(roleName);
            role.setPyCode(userNamePinyin);
        }
        // 生成拼音码
        if("".equals(StringUtils.safeToString(role.getWbCode(),"").replaceAll(" ",""))) {
            String userNameWubi = WubiUtils.getSimpleWuBi(roleName);
            role.setWbCode(userNameWubi);
        }
    }

    /**
     * 删除角色
     * @param roleCode 角色编码
     * @return
     */
    @Override
    public Boolean remove(String roleCode) {
        notNull(roleCode,"角色编码不能为空");

        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Role::getRoleCode,roleCode);
        Role localRole = this.mapper.selectOne(lqw);

        if(localRole == null){
            throw new ServiceException("未获取到需要删除的角色信息");
        }

        List<User> users = userService.queryUserByRoleCode(roleCode);

        if(users.size() > 0){
            throw new ServiceException("已有用户维护此角色，不允许删除");
        }

        //删除角色，删除已维护的角色对应菜单信息
        roleVsMenuService.deleteByRoleCode(roleCode,null);

        if(this.mapper.deleteById(localRole) > 0){
            return true;
        }

        throw new ServiceException("删除失败");
    }


    /**
     * 角色列表
     * @param queryPage 分页
     * @param searchParam 名称
     * @param isShowStop  是否显示停用
     * @return
     */
    @Override
    public IPage<Role> queryList(Page<Role> queryPage, String searchParam, String isShowStop) {

        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();

        if(StringUtils.notEmptyParams(searchParam)){
            lqw.like(Role::getRoleCode,searchParam)
                    .or().like(Role::getPyCode,searchParam)
                    .or().like(Role::getWbCode,searchParam)
                    .or().like(Role::getRoleName,searchParam);
        }
        lqw.orderByDesc(Role::getRoleCode);
        return this.mapper.selectPage(queryPage,lqw);
    }

    /**
     * 角色信息详情
     * @param roleCode 角色编码
     * @return
     */
    @Override
    public Role detail(String roleCode) {

        notNull(roleCode,"角色编码不能为空");

        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Role::getRoleCode,roleCode);

        return  this.mapper.selectOne(lqw);
    }
}
